// FacebookApplicationSettingsCollection.cs
// Facebook/FrameworkWeb/Web
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Configuration;
using System.Diagnostics;
using System.Web.Configuration;

namespace Facebook.Web.Configuration {

    /// <summary>
    /// The list of configured Facebook applications.
    /// </summary>
    [ConfigurationCollection(typeof(FacebookApplicationSettings), AddItemName = "application")]
    public class FacebookApplicationSettingsCollection : ConfigurationElementCollection {

        private static readonly ConfigurationPropertyCollection AllProperties =
            new ConfigurationPropertyCollection();

        /// <internalonly />
        public override ConfigurationElementCollectionType CollectionType {
            get {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        /// <internalonly />
        protected override string ElementName {
            get {
                return "application";
            }
        }

        /// <internalonly />
        protected override ConfigurationPropertyCollection Properties {
            get {
                return AllProperties;
            }
        }

        /// <summary>
        /// Gets or sets a FacebookApplicationSettings object by index.
        /// </summary>
        /// <param name="index">The specified index.</param>
        /// <returns>The FacebookApplicationSettings object at the specified index.</returns>
        public FacebookApplicationSettings this[int index] {
            get {
                return (FacebookApplicationSettings)BaseGet(index);
            }
            set {
                if (BaseGet(index) != null) {
                    BaseRemoveAt(index);
                }

                BaseAdd(index, value);
            }
        }

        /// <summary>
        /// Gets a FacebookApplicationSettings object by name.
        /// </summary>
        /// <param name="name">The specified name.</param>
        /// <returns>The FacebookApplicationSettings object at the specified index.</returns>
        public new FacebookApplicationSettings this[string name] {
            get {
                return (FacebookApplicationSettings)BaseGet(name);
            }
        }

        /// <summary>
        /// Adds the specified application to the list.
        /// </summary>
        /// <param name="application">The application to be added.</param>
        public void Add(FacebookApplicationSettings application) {
            BaseAdd(application);
        }

        /// <summary>
        /// Removes the specified application from the list.
        /// </summary>
        /// <param name="application">The application to be removed.</param>
        public void Remove(FacebookApplicationSettings application) {
            BaseRemove(GetElementKey(application));
        }

        /// <summary>
        /// Clears the list of applications.
        /// </summary>
        public void Clear() {
            BaseClear();
        }

        /// <internalonly />
        protected override ConfigurationElement CreateNewElement() {
            return new FacebookApplicationSettings();
        }

        /// <internalonly />
        protected override Object GetElementKey(ConfigurationElement element) {
            return ((FacebookApplicationSettings)element).Name;
        }
    }
}
